﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Controls;
using AvalonDock;
using DataGenerator.DefaultSets;
using Dispatcher.Extensions;
using Dispatcher.Helpers;
using Dispatcher.ViewModels.Base;
using Dispatcher.Views;
using Repository;

namespace Dispatcher.ViewModels
{
    /// <summary>
    /// Main Window View Model.
    /// </summary>
    public class MainViewModel : ViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="MainViewModel"/> class.
        /// </summary>
        /// <param name="dockingManager">The docking manager.</param>
        public MainViewModel(DockingManager dockingManager)
        {
            ModelContainer = new ModelContainer();
            Documents = new ObservableCollection<DocumentContent>();

            var objectSets = ModelContainer.GetObjectSets().ToList();
            ObjectSetMenuItems = objectSets.Select(s => new MenuItem {Tag = s, Header = s.ElementType.Name}).ToList();

            foreach (var menuItem in ObjectSetMenuItems)
            {
                menuItem.Click +=
                    (sender, args) =>
                        {
                            var item = (MenuItem)sender;
                            var objectSet = (IQueryable)item.Tag;
                            var viewModel = ViewModelFactory.CreateObjectSetViewModel(objectSet.ElementType, ModelContainer);
                            var view1 = new DocumentListView {Title = objectSet.ElementType.Name};
                            viewModel.BindToView(view1);
                            Documents.Add(view1);
                        };
            }

            var model = new ObjectContextTreeViewModel(ModelContainer);
            var view = new CommonTreeView();
            model.BindToView(view);
            view.Show(dockingManager);
            view.Hide();
        }

        /// <summary>
        /// Gets or sets the context.
        /// </summary>
        /// <value>
        /// The context.
        /// </value>
        public ModelContainer ModelContainer { get; set; }

        /// <summary>
        /// Gets or sets the status.
        /// </summary>
        /// <value>
        /// The status.
        /// </value>
        public string Status
        {
            get
            {
                return Get(() => Status);
            }

            set
            {
                Set(() => Status, value);
            }
        }

        /// <summary>
        /// Gets or sets the documents.
        /// </summary>
        /// <value>
        /// The documents.
        /// </value>
        public ObservableCollection<DocumentContent> Documents { get; set; }

        /// <summary>
        /// Gets or sets the entity sets menu items source.
        /// </summary>
        /// <value>
        /// The entity sets menu items source.
        /// </value>
        public List<MenuItem> ObjectSetMenuItems { get; set; }

        /// <summary>
        /// Called when [load].
        /// </summary>
        public void OnLoad()
        {
            /*
            foreach (var item in ObjectSetMenuItems)
            {
                var objectSet = (IQueryable)item.Tag;
                var viewModel = ViewModelFactory.CreateObjectSetViewModel(objectSet.ElementType, ModelContainer);
                var view1 = new DocumentListView { Title = objectSet.ElementType.Name };
                viewModel.BindToView(view1);
                Documents.Add(view1);
            }
            */


            var nowDate = DateTime.Now.Date;
            var m = new PlaceSearchViewModel(ModelContainer)
            {
                FromDate = nowDate,
                ToDate = nowDate,
                FromTime = new TimeSpan(9, 45, 00),
                ToTime = new TimeSpan(11, 00, 00)
            };

            var v = new PlaceSearchView {Title = "Place search"};
            m.BindToView(v);
            Documents.Add(v);
            v.Activate();
            //m.Search();

            var view = new PlaceTimetableDocumentView();

            var personGroupTypeSet = new PersonGroupTypeSet(ModelContainer.PersonGroupTypeSet);
            var department = ModelContainer.PersonGroupSet.
                FirstOrDefault(g => g.PersonGroupType.Id == personGroupTypeSet.Department.Id);

            var from = nowDate;
            var to = from.AddDays(5);
            var viewModel = new PlaceTimetableViewModel(ModelContainer)
                                {FromDate = from, ToDate = to, PersonGroup = department};
            view.Bind(viewModel);
            Documents.Add(view);
            view.Activate();            
        }
    }
}
